home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / PubSc / images.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  5.4 KB  |  226 lines

  1. /* images.c -- image utiltity routines :ts=4 */
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include "sysall.h"
  14.  
  15. #define D(x)    ;
  16. #define DI(x)    ;
  17.  
  18. /*
  19.  * will set up Image.Depth.  Feel free
  20.  * to change the PlanePick and PlaneOnOff fields
  21.  */
  22. struct Image    *
  23. CreateImage( width, height, depth )
  24. {
  25.     struct Image    *im;
  26.     int    rassize;
  27.  
  28.     im = (struct Image *) AllocMem( sizeof (struct Image),
  29.             MEMF_PUBLIC | MEMF_CLEAR );
  30.  
  31.     D( printf("CE: w %d h %d, im %lx, next %lx\n", width, height, im,
  32.         im->NextImage ) );
  33.  
  34.     if ( im )
  35.     {
  36.         rassize = RASSIZE( (long) width, (long) height );
  37.         D( printf( "CreateImage: rassize: %d\n", rassize ) );
  38.  
  39.         im->ImageData = (USHORT *) AllocVec( (long) rassize * depth,
  40.             (long) MEMF_CHIP | MEMF_CLEAR );
  41.  
  42.         if ( im->ImageData )
  43.         {
  44.             im->Width = width;
  45.             im->Height = height;
  46.             im->Depth = depth;
  47.             im->PlanePick = ( 1 << depth ) - 1;
  48.         }
  49.         else
  50.         {
  51.             D( printf("CI free abort\n") );
  52.             FreeMem( im, (long) (sizeof *im) );
  53.             im = NULL;
  54.         }
  55.     }
  56.     return ( im );
  57. }
  58.  
  59. /*
  60.  * dont' rely on depth for raster size, since he's allowed to
  61.  * change that to pull mask tricks
  62.  */
  63. DeleteImage( im )
  64. struct Image    *im;
  65. {
  66.     D( printf("DeleteImage\n") );
  67.     FreeVec( im->ImageData );
  68.     FreeMem( im, (long) sizeof ( *im ) );
  69. }
  70.  
  71. /* area info capacity (MODIFY if you need more space)    */
  72. #define MAXAVECTORS    (20)
  73. #define AIBUFFSIZE    (5 * MAXAVECTORS)
  74.  
  75. #define IRP_SIZE ( (long) sizeof ( struct RastPort ) + \
  76.     sizeof ( struct BitMap ) + \
  77.     sizeof ( struct TmpRas ) + \
  78.     sizeof ( struct AreaInfo )  + AIBUFFSIZE )
  79.  
  80. /*
  81.  * create a rastport for drawing into an Intuition Image structure
  82.  * creates it with an initialized TmpRas and AreaInfo.
  83.  * modify this if you need more space in the AreaInfo.
  84.  */
  85. struct RastPort    *
  86. CreateImageRPort( im )
  87. struct Image    *im;
  88. {
  89.    /** These are all allocated contiguously    **/
  90.    struct RastPort    *rp;
  91.    struct BitMap    *bmap;
  92.    struct TmpRas    *tmpras;
  93.    struct AreaInfo    *ainfo;
  94.    char            *abuffer;
  95.  
  96.    int    i;
  97.    int    rassize;
  98.    UBYTE    *planeptr;
  99.    UBYTE    *tmpraster;
  100.  
  101.  
  102.    /* allocate rport and bmap contiguously    */
  103.    rp = (struct RastPort *) AllocMem( (long) IRP_SIZE, (long) MEMF_CLEAR );
  104.  
  105.    rassize = RASSIZE( (long) im->Width, (long) im->Height );
  106.  
  107.    D( printf( "CIRP: irpsize: %ld, rassize: %d\n", IRP_SIZE, rassize ) );
  108.  
  109.    if ( rp )
  110.    {
  111.     D( printf("cirp: rastport at: %lx\n", rp ) );
  112.     InitRastPort( rp );
  113.  
  114.     bmap = (struct BitMap *) &rp[1];
  115.     tmpras = (struct TmpRas *) &bmap[1];
  116.     ainfo = (struct AreaInfo *) &tmpras[1];
  117.     abuffer = (char *) &ainfo[1];
  118.  
  119.     /*** init the BitMap ***/
  120.  
  121.     D( printf( "cirp: init the bmap\n") );
  122.  
  123.     InitBitMap(  bmap, (long) im->Depth, 
  124.         (long) im->Width, (long) im->Height );
  125.  
  126.     /* point rastport at bitmap    */
  127.     rp->BitMap = bmap;
  128.  
  129.     /* point bitmap at image data    */
  130.     planeptr = (UBYTE *) im->ImageData;
  131.     for ( i = 0; i < im->Depth; ++i )
  132.     {
  133.         bmap->Planes[ i ] = (PLANEPTR) planeptr;
  134.         D( printf( "cirp: init plane: %lx\n", planeptr) );
  135.         planeptr += rassize;
  136.     }
  137.  
  138.     /*** init the AreaInfo ***/
  139.     D( printf("init the AInfo\n") );
  140.     InitArea( ainfo, abuffer, (long) MAXAVECTORS );
  141.     rp->AreaInfo = ainfo;
  142.  
  143.     /*** init the TmpRas ***/
  144.     D( printf("init the TmpRas\n") );
  145.     tmpraster = (UBYTE *) AllocMem( (long) rassize, (LONG) MEMF_CHIP );
  146.     if ( tmpraster )
  147.     {
  148.         D( printf("TR: %d bytes at %lx\n", rassize, tmpraster ) );
  149.         InitTmpRas( tmpras, tmpraster, (long) rassize );
  150.         rp->TmpRas = tmpras;
  151.         rp->RP_User = (APTR *) tmpraster;    /* for later freeing    */
  152.     }
  153.     else
  154.     {
  155.         D( printf("no tmpraster\n") );
  156.         FreeMem( rp, IRP_SIZE );
  157.         rp = NULL;
  158.     }
  159.    }
  160.    D( else printf("no rastport bundle\n") );
  161.  
  162.    D( printf("cirp: returning rastport %lx\n", rp ) );
  163.    return ( rp );
  164. }
  165.  
  166. /*
  167.  * only use on rastports created by CreateImageRPort
  168.  */
  169. DeleteImageRPort( rp )
  170. struct RastPort    *rp;
  171. {
  172.     WaitBlit();        /* tmpras no longer in use    */
  173.  
  174.     D( printf("DIRP: %lx\n", rp ) );
  175.  
  176.     D( printf("delete tmpraster at %lx\n", rp->RP_User ) );
  177.     D( printf("trsize: %d\n", rp->BitMap->Rows * rp->BitMap->BytesPerRow ) );
  178.  
  179.     /* tmpraster is same size as rastport bitmap    */
  180.     FreeMem( rp->RP_User, (long) rp->BitMap->Rows * rp->BitMap->BytesPerRow );
  181.  
  182.     D( printf("delete rastport at %lx\n", rp ) );
  183.  
  184.     FreeMem( rp, (long) IRP_SIZE );
  185. }
  186.  
  187. /*
  188.  * return pointer to one of the image planes
  189.  */
  190. UWORD    *
  191. ImagePlane( im, plane )
  192. struct Image    *im;
  193. {
  194.     int    planesize = RASSIZE( im->Width, im->Height );
  195.  
  196.     DI( printf("IP: planesize: %d\n", planesize ) );
  197.  
  198.     return ( im->ImageData + plane * (planesize/2) );
  199. }
  200.  
  201. clearWords( buff, numwords )
  202. WORD    *buff;
  203. {
  204.     while ( numwords-- ) *buff++ = 0;
  205. }
  206.  
  207. /* fancy this up later    */
  208. drawBox( rp, width, height, fieldcolor, bordercolor, vthick, hthick )
  209. struct RastPort    *rp;
  210. {
  211.     DI( printf("w %d h %d vthick %d hthick %d\n",
  212.         width, height, vthick, hthick ) );
  213.     SetAPen( rp, (long) bordercolor );
  214.     RectFill( rp, 0L, 0L, (long) width-1, (long) height-1 );
  215.  
  216.     SetAPen( rp, (long) fieldcolor );
  217.     RectFill( rp, (long) vthick, (long) hthick, (long) width-1 - vthick,
  218.         (long) height - 1 - hthick );
  219.  
  220.     DI( printf("outer rect: %d %d %d %d\n", 0, 0, width-1,
  221.         height - 1) );
  222.  
  223.     DI( printf("inner rect %d %d %d %d\n",
  224.         vthick, hthick, width-1 - vthick, height - 1 - hthick ) );
  225. }
  226.